<template>
	<view>
		<div>
			<navitop>
				<i class="iconfont" slot="left-slot" @click="outlot">&#xe667;</i>
				<span slot="center-slot">新增联系人</span>
				<!-- <i class="iconfont " slot="right-slot" @click="add">确定</i> -->
			</navitop>
		</div>
		<div class="public_list_out">
			
			<div :style="'margin-top:' + nv_height"></div>

				<div >
					<div class="public_list">
						<text>客户名称:</text><input type="text" v-model="kehu_name" disabled=true>
					</div>
				</div>
				
				<div  v-for="(item,index) in dataSorce">
						
					<div class="public_list" v-if="item.code !== 'gender' && item.code !== 'birthday' && item.code !== 'createDate'">
						<div class=" public_list_left">
							<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
							<text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">
							<input type="text" v-model="item.val">
						</div>	
					</div>
					<div class="public_list" v-else-if="item.code == 'birthday'">
						<div class=" public_list_left">
							<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
							<text>{{item.name}}:</text>
						</div>
						<div class="public_list_right">	
							<kxTime :date='date' :start='1' @rundata='datetime' :default_time="1" default='start' ></kxTime>
						</div>	
					</div>
					<div class="public_list" v-else-if="item.code == 'gender'">
						<div class=" public_list_left">
							<text v-if="item.onAppFill == 1" style="color: red; ">*</text>
							<text>{{item.name}}</text>
						</div>
						<div class="public_list_right">		
							<radio-group @change="radioChange"  style="transform:scale(0.7);">
								 <radio value="男">
									 <text >男</text>
								 </radio>
								 <radio style="margin-left: 50px;"  value="女">
									 <text  >女</text>
								 </radio>
							</radio-group>	
						</div>	
					</div>
				</div>
		</div><br><br>
		<div :style="heigsdiv"></div>
		<div class="public_button" :style="heigs">
			<div class="public_text" @click="add">
				保&nbsp;&nbsp;存
			</div>
		</div>
	</view>
</template>

<script>
	import navitop from '@/components/common/navi-top'
	import kxTime from "@/components/kx-datetime/kx-datetime"
	import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
	import ztree from '@/pages/common/common/city/z_citys.vue'
	import {detail,save,namelist,distuser,list} from '@/common/api/job/jobapi.js'
	import {Z_threes} from '@/common/api/common/public_api.js'
	export default {
		components:{
			navitop,
			popus,
			kxTime,
			ztree
		},
		data(){
			return {
				dataSorce: [],
				sex: '',
				uid: '',
				kehu_name: '',
				startdate: "1930" ,
				enddate: "", 
				zhuke: '',
				data: '',
				date: '',
				url:'',
				heigs: '',
				heigsdiv: 0,
				nv_height:''
			}
		},
		mounted() {
			uni.onKeyboardHeightChange(res => {
				res.height = res.height + 50;
				this.heigsdiv = 'height:' + res.height  + 'px;' ;
				this.heigs = 'height:' + res.height + 'px;' ;
			})
			
			this.nv_height = uni.getSystemInfoSync().statusBarHeight + 75 + 'px';
		
			this.getpost();
		},
		onLoad(option) {
			this.uid = option.id;
			this.kehu_name = option.names;
			this.url = '&classid=' + option.classid + '&crmType=' + option.crmType + '&name=' + option.name + '&add_class=' + 'contactadd';
		},
		

		methods:{
			outlot(){
				uni.navigateBack();
			},
			alertcity(){
				this.$refs.alert.z_three();
			},
			
			citys(){
				let children = [];
				city({current: 1,size: -1}).then((result)=> {
					this.citys_c = Z_threes(result.data,[])
				})
			},
			
			
			datetime(e){
				this.date = e;
			},
			getpost(){
				list({source: 'linkMan'}).then((result) => {
					for(let i in result.data){
						if(result.data[i].code !== "createUserId" && result.data[i].code !== "crmName" && result.data[i].code !== "isMain"){
							this.dataSorce.push(result.data[i]);
						}
					}
				})
			},
			add(){
				let stri = {};
				let pas = {};
				uni.showLoading({
					title: '加载中。。。'
				})
				for(let i in this.dataSorce){
					if(this.dataSorce[i].code == 'gender'){
						this.dataSorce[i].val = this.sex;
					}
					if(this.dataSorce[i].onAppFill){
						if(this.dataSorce[i].val){
							stri[this.dataSorce[i].code] = this.dataSorce[i].val
						}else {
							uni.showToast({
								title: '请输入'+ this.dataSorce[i].name,
								icon: 'error'
							});
							return false;
						}
					}else {
						stri[this.dataSorce[i].code] = this.dataSorce[i].val
					}
				}
				
				this.dataSorce.forEach(item =>{
					if(item.dataType == 2){
						
						pas[item.id] = item.val
					}
				})
				
				
				stri['defineFields'] =JSON.stringify(pas);
				stri['crmName'] = this.kehu_name;
				stri['crmId'] = this.uid;
				// if(!this.zhuke){
				// 	uni.showToast({
				// 				title: '请选择主要联系人',
				// 				icon: 'error'
				// 			}) 
				// 			return false;
				// }
				// stri['isMain'] = this.zhuke.detail.value;
				stri['birthday'] = this.date.substr(0,10);
				console.log(stri)
				distuser(stri).then((val) => {
					if(val.code == 200){
						uni.hideLoading();
						uni.showToast({
							title: '添加成功'
						})
						setTimeout(() => {
							uni.navigateTo({
							      url: '/pages/common/index/detial?id=' + this.uid + this.url + "&index=" + 1 
							});
						},1500)
					}
				})
				
			},
			radioChange(evt) {
				this.sex = evt.detail.value
			},
			radioChanges(val){
				this.zhuke = val;
			}
		}
	}
	
</script>

<style scoped lang="scss">

</style>
